﻿using Dapper;
using HrEFCore;
using HrEFCore.Models;
using HrIService;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;

namespace HrService
{
	public class SSService : ISSService
	{
		private readonly MyDbContext myDbContext;

		public SSService(MyDbContext myDbContext)
        {
			this.myDbContext = myDbContext;
		}
		//根据id查询
		public async Task<SS> SSById(string StandardId)
		{
			return await myDbContext.SS.FirstAsync(e=>e.StandardId== StandardId);
			
		}
		public async Task<SSandSSD> SSandSSDById(short id)
		{
			return await myDbContext.SSandSSD.FirstOrDefaultAsync(e => e.SdtId == id);
		}
		//分页查询
		public async Task<FenYe<SS>> SSFenYeAsync(FenYe<SS> fenYe)
		{
			using (var conn = myDbContext.Database.GetDbConnection())
			{
				//@pageSize, @keyName, @tableName, @currentPage, @rows
				var parameters = new DynamicParameters();
				#region 另种写法
				//FenYe<SS> fenYe
				parameters.Add("@currentPage", value: fenYe.PageIndex);
				parameters.Add("@pageSize", value: fenYe.PageSize);
				parameters.Add("@keyName", value: "SsdId");
				parameters.Add("@tableName", value: "SS");
				parameters.Add("@rows", direction: ParameterDirection.Output, dbType: DbType.Int32);
				string sql = " exec [dbo].[proc_FenYe]  @currentPage, @pageSize, @keyName,  @tableName,  @rows OUTPUT";
				fenYe.List = await conn.QueryAsync<SS>(sql, parameters);
				fenYe.Rows = parameters.Get<int>("rows");
				return fenYe;
				#endregion
				#region 
				//int currentPage, int pageSize
				//parameters.Add("currentPage", dbType: DbType.Int32, direction: ParameterDirection.Input, value: currentPage);
				//parameters.Add("pageSize", dbType: DbType.Int32, direction: ParameterDirection.Input, value: pageSize);
				//parameters.Add("keyName", dbType: DbType.String, direction: ParameterDirection.Input, value: "SsdId");
				//parameters.Add("tableName", dbType: DbType.String, direction: ParameterDirection.Input, value: "SS");
				//parameters.Add("rows", dbType: DbType.Int32, direction: ParameterDirection.Output);
				//string sql = "exec [dbo].[proc_FenYe]";
				//IEnumerable<SS> list = await conn.QueryAsync<SS>(sql, parameters, commandType: CommandType.StoredProcedure);
				//int rows = parameters.Get<int>("@rows");
				//FenYe<SS> fenYe = new FenYe<SS>()
				//{
				//	List = list,
				//	Rows = rows
				//};
				//if (fenYe != null)
				//{
				//	return fenYe;
				//}
				//else
				//{
				//	return null;
				//}
				#endregion
			}
		}
		//模糊查询
		public async Task<FenYe<SSWhere>> SSFenYeWhereAsync(int currentPage, int pageSize, string where)
		{
			//using (var conn = myDbContext.Database.GetDbConnection())
			//{
			//	//@pageSize,@keyName,@tableName,@where,@currentPage,@lie ,@rows
			//	var parameters = new DynamicParameters();
			//	parameters.Add("pageSize",dbType:DbType.Int32, direction: ParameterDirection.Input, value: pageSize);
			//	parameters.Add("keyName", dbType: DbType.String, direction: ParameterDirection.Input, value: "SsdId");
			//	parameters.Add("tableName", dbType: DbType.String, direction: ParameterDirection.Input, value: "SS");
			//	parameters.Add("where", dbType: DbType.String, direction: ParameterDirection.Input, value: where);
			//	parameters.Add("currentPage", dbType: DbType.Int32, direction: ParameterDirection.Input, value: currentPage);
			//	parameters.Add("lie", dbType: DbType.String, direction: ParameterDirection.Input, value: "*");
			//	parameters.Add("rows", dbType: DbType.Int32, direction: ParameterDirection.Output);
			//	string sql = "[dbo].[proc_Fenye2]";
			//	IEnumerable<SSWhere> list = await conn.QueryAsync<SSWhere>(sql, parameters, commandType: CommandType.StoredProcedure);
			//	int rows = parameters.Get<int>("@rows");
			//	FenYe<SSWhere> fenYe = new FenYe<SSWhere>()
			//	{
			//		List = list,
			//		Rows = rows
			//	};
			//		return fenYe;
			//}
			using (var conn = myDbContext.Database.GetDbConnection())
			{
				DynamicParameters dp = new DynamicParameters();
				dp.Add("@pageSize", pageSize);
				dp.Add("@keyName", "SsdId");
				dp.Add("@tableName", "SS");
				dp.Add("@where", where);
				dp.Add("@currentPage", currentPage);
				dp.Add("@lie", "*");
				dp.Add("@rows", direction: ParameterDirection.Output, dbType: DbType.Int32);
				string sql = "exec [dbo].[proc_FenYe2] @pageSize,@keyName,@tableName,@where,@currentPage,@lie,@rows out";
				IEnumerable<SSWhere> list = await conn.QueryAsync<SSWhere>(sql, dp);
				int rows = dp.Get<int>("rows");
				FenYe<SSWhere> fy = new FenYe<SSWhere>()
				{
					List = list,
					Rows = rows
				};
				return fy;
			}
		}
		//添加
		public async Task<int> SSInsertAsync(SS ss)
		{
			myDbContext.SS.AddAsync(ss);
			return await myDbContext.SaveChangesAsync();
		}
		public async Task<int> SSDInsertAsync(SSD ssd)
		{
			myDbContext.SSD.AddAsync(ssd);
			return await myDbContext.SaveChangesAsync();
		}
		//public async Task<int> SSInsertAsync(SS ss,SSD ssd)
		//{
		//	using (var conn = myDbContext.Database.GetDbConnection())
		//	{
		//		var parameters = new DynamicParameters();

		//		//parameters.Add("pageSize", dbType: DbType.Int32, direction: ParameterDirection.Input, value: pageSize);//输入参[Remark]
		//		parameters.Add("StandardName", dbType:DbType.String,direction:ParameterDirection.Input,value:ss.StandardName);
		//		parameters.Add("SalarySum", dbType: DbType.Decimal, direction: ParameterDirection.Input, value: ss.SalarySum);
		//		parameters.Add("Designer", dbType: DbType.String, direction: ParameterDirection.Input, value: ss.Designer);
		//		parameters.Add("Register", dbType: DbType.String, direction: ParameterDirection.Input, value: ss.Register);
		//		parameters.Add("RegistTime", dbType: DbType.DateTime, direction: ParameterDirection.Input, value: ss.RegistTime);
		//		parameters.Add("Remark", dbType: DbType.String, direction: ParameterDirection.Input, value: ss.Remark);
		//		parameters.Add("CheckStatus", dbType: DbType.Int32, direction: ParameterDirection.Input, value: 0);
		//		parameters.Add("ChangeStatus", dbType: DbType.Int32, direction: ParameterDirection.Input, value: 0);
		//		parameters.Add("ItemId", dbType: DbType.Int16, direction: ParameterDirection.Input, value: ssd.ItemId);
		//		parameters.Add("ItemName", dbType: DbType.String, direction: ParameterDirection.Input, value: ssd.ItemName);
		//		parameters.Add("Salary", dbType: DbType.Decimal, direction: ParameterDirection.Input, value: ssd.Salary);
		//		//@StandardName,@SalarySum,@Designer,@UId,@RegistTime,@Remark,@CheckStatus,@ItemId,@ItemName,@Salary
		//		string sql = "[dbo].[xinchou] ";
		//		int row =await conn.ExecuteAsync(sql, parameters, commandType: CommandType.StoredProcedure);
		//		if (row > 0)
		//		{
		//			return row;
		//		}
		//		else
		//		{
		//			return 0;
		//		}
		//	}
		//}
		//查询所有
		public Task<List<SS>> SSSelectAsync()
		{
			return myDbContext.SS.ToListAsync();
		}
		//下拉列表
		public async Task<List<SS>> SSXL()
		{
			List<SS> list;
			using (var conn = myDbContext.Database.GetDbConnection())
			{
				string sql = "select * from view_1";
				 list = (List<SS>)await conn.QueryAsync<SS>(sql);
				return list;
			}
		}
		//修改复核
		public async Task<int> SSFHUpdate(SS ss)
		{
			var s = myDbContext.SS.Attach(ss);
			s.Property(e=>e.Checker).IsModified=true;
			s.Property(e=>e.CheckTime).IsModified=true;
			s.Property(e=>e.CheckComment).IsModified=true;
			s.Property(e=>e.CheckStatus).IsModified=true;
			return await myDbContext.SaveChangesAsync();
		}
		//修改变更
		public async Task<int> SSBGUpdate(SS ss)
		{
			var s = myDbContext.SS.Attach(ss);
			s.Property(e => e.StandardName).IsModified = true;
			s.Property(e => e.SalarySum).IsModified = true;
			s.Property(e => e.Designer).IsModified = true;
			s.Property(e => e.Changer).IsModified = true;
			s.Property(e => e.ChangeTime).IsModified = true;
			s.Property(e => e.Remark).IsModified = true;
			s.Property(e => e.ChangeStatus).IsModified = true;
			s.Property(e => e.CheckStatus).IsModified = true;
			return await myDbContext.SaveChangesAsync();
		}
	}
}
